<?php

defined('BASEPATH') OR exit('No direct script access allowed');

include_once ( APPPATH . "libraries/passwordhash.php" );

class Prihlasenie extends MY_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('prihlasenie_model');
	}
	
	public function index()
	{
		$this->showView("prihlasenie");
	}
	
	public function prihlasit()
	{
		$email = $_POST["email"];
		if (! isset($email))
		{
			$this->displayError(lang("emptyLogin"));
			return;
		}
		
		$password = $_POST["password"];
		if (! isset($password))
		{
			$this->displayError(lang("emptyPassword"));
			return;
		}
		
		$customer = $this->prihlasenie_model->loginCustomer($email);
		if (! $customer)
		{
			$this->displayError(lang("loginNotExists"));
			return;
		}
		
		switch ($customer->state)
        {
            case 0:
                $this->displayError(lang("userNotActivated"));
                return;
            
            case 1:
                break;
            
            case 2:
                $this->displayError(lang("userIsBlocked"));
                return;
            
            default:
                return;
        }
		
		if (!PasswordHash::verifyPasswordHash( $password, $customer->passwordsalt, $customer->passwordhash ))
		{
			$this->displayError(lang("invalidPassword"));
            return;
		}
		
		$this->session->set_userdata('loggedCustomer', serialize($customer));
        WebStat::logStatistic( StatisticType::WEB_STAT_LOGIN_USER, $customer->email );
	}
}

?>
